package com.bootssm.controller;

import com.bootssm.entity.*;
import com.bootssm.service.HolidayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/holiday")
public class HolidayController {
    @Autowired
    HolidayService holidayService;

    @RequestMapping("/findAll")
    public PageView<Holiday> findAll(@RequestBody HolidayVO holidayVO){
        Integer page = holidayVO.getPage();
        Integer start = (page-1)*holidayVO.getSize();
        holidayVO.setStart(start);

        List<Holiday> holidayList = holidayService.findAll(holidayVO);

        Integer count = holidayService.getCount(holidayVO);

        PageView<Holiday> pageView = new PageView<>();
        pageView.setDataList(holidayList);
        pageView.setCount(count);
        return pageView;
    }
    @PostMapping("/deleteById")
    public Result deleteById(@RequestBody Holiday holiday) {
        String id = holiday.getHolidayid();
        holidayService.deleteById(id);
        return new Result("200", "删除成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Holiday holiday) {
        try {
            holidayService.update(holiday);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500","修改失败");
        }
        return new Result("200","修改成功");
    }
    @PostMapping("/save")
    public Result save(@RequestBody HolidayVO holidayVO) {
        try {
            holidayService.save(holidayVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "保存失败");
        }
        return new Result("200", "保存成功");
    }
}
